package com.itany.ems.dao.impl;

import com.itany.ems.dao.EmpDao;
import com.itany.ems.entity.Emp;
import com.itany.ems.exception.DataAccessException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 石小俊
 * @date 2024年05月24日 18:42
 */
public class EmpDaoImpl implements EmpDao {
    @Override
    public void insertEmp(Emp emp) throws DataAccessException {
        String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, "root", "");
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append("   (name,salary,gender,edu,hobbies)")
                    .append(" values ")
                    .append("   (?,?,?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setDouble(2,emp.getSalary());
            ps.setString(3,emp.getGender());
            ps.setString(4,emp.getEdu());
            ps.setString(5,emp.getHob());

            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataAccessException("数据访问异常");
        }
    }
}
